package com.gameShare.service.game

import com.gameShare.entity.dto.game.GameCardDto
import com.gameShare.entity.po.game.GameInfo
import com.gameShare.entity.vo.PageVo
import com.gameShare.enums.misc.OrderType
import com.gameShare.enums.misc.ResponseCodeEnum
import com.gameShare.enums.gameInfo.GameInfoOrderEnum
import com.gameShare.exceptions.BusinessException
import com.gameShare.mappers.game.GameInfoMapper
import com.gameShare.mappers.game.GameTagsMapper
import org.springframework.stereotype.Service

@Service
class GameInfoService(
    private val gameInfoMapper: GameInfoMapper,
    private val gameTagsMapper: GameTagsMapper
){


    /**
     * 获取卡片信息
     *
     * @param year 年份
     * @param monthList 月份列表
     * @param gameType 游戏类型
     * @param gameLang 游戏语言
     * @param gamePlatform 游戏平台
     * @param pageNo 页数
     * @param pageSize 页面大小
     * @param order 排序字段
     * @param orderType 排序类型
     * @return pageVo对象里面包含了gameCardDto列表
     */
    fun getGameCardPage(year : String?, monthList : String?, gameType : Int?, gameLang : Int?,gamePlatform : Int?, pageNo : Int?, pageSize : Int?, order : Int?, orderType: Int?): PageVo<GameCardDto?> {

        //检查monthList是否为 1,3,5这种格式的且不为空
        val monthListRegex = Regex("^(1[0-2]|[1-9])(,(1[0-2]|[1-9]))*$")
        val monthListJson = if (monthList != null) {
            // 不符合格式的直接抛出异常
            if(!monthListRegex.matches(monthList)){
                throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
            }
            // 反则处理成列表形式
            val monthListJson = monthList.split(",")
            monthListJson
                .ifEmpty { throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR) }
                .map { it.trim() }
        } else null

        //拼凑orderBy条件order为空则直接用post_time排序,不为空则用用户输入的值排序，同理排序是升序还是降序用用户输入的排,默认为asc
        val orderBy: String = when {
            order == null -> "${GameInfoOrderEnum.POST_TIME.field} ${OrderType.ASC.type}"
            else -> {
                val orderField = GameInfoOrderEnum.getByCode(order)?.field
                    ?: throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
                val orderTypeField = orderType?.let { OrderType.getByCode(it)?.field }
                    ?: OrderType.ASC.type
                "$orderField $orderTypeField"
            }
        }

        return PageVo.generatePageVo(
            pageNo,
            pageSize,
            { gameInfoMapper.selectBaseInfoCount(year, monthListJson, gameType, gameLang, gamePlatform) },
            { start, end ->
                gameInfoMapper.selectBaseInfoPage(year, monthListJson, gameType, gameLang, gamePlatform, start, end, orderBy)
                    .onEach { item -> item.tags = gameTagsMapper.selectTagNameByGameId(item.id) }
            }
        )
    }

    fun getRandomInfo(): GameInfo {
        return gameInfoMapper.selectRandomInfo()
    }


//    fun getGameInfoDetail(){
//
//    }

    /**
     * 检查gameInfo是否存在通过gameId
     *
     * @param gameId
     */
    fun checkGameInfoExist(gameId : String){
        gameInfoMapper.selectById(gameId)
            ?: throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
    }
}